package com.mineplatform.dao;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.mineplatform.domain.M_Token;
import com.mineplatform.domain.M_User;

@Repository
public class M_TokenDao extends M_BaseDao {

	public void save(M_Token token) {
		super.getHibernateTemplate().save(token);
	}

	public M_Token findByToken(String token) {
		DetachedCriteria crit = DetachedCriteria.forClass(M_Token.class);
		crit.add(Restrictions.eq("Id", token));
		List<?> list = super.getHibernateTemplate().findByCriteria(crit);
		if (list.size() > 0) {
			return (M_Token) list.get(0);
		} else {
			return null;
		}
	}

	public M_Token findEffectiveByUserId(String userId) {
		DetachedCriteria crit = DetachedCriteria.forClass(M_Token.class);
		crit.add(Restrictions.eq("UserId", userId));
		crit.add(Restrictions.eq("Enable", true));
		List<?> list = super.getHibernateTemplate().findByCriteria(crit);
		if (list.size() > 0) {
			return (M_Token) list.get(0);
		} else {
			return null;
		}
	}

	public void update(M_Token token) {
		super.getHibernateTemplate().update(token);
	}
}
